//
//  ViewController.m
//  AppleCilent
//
//  Created by Nail Sharipov on 01/04/14.
//  Copyright (c) 2014 Nail Sharipov. All rights reserved.
//

#import "MainViewController.h"
#import "MenuListContent.h"
#import "CartManager.h"
#import "CartTableView.h"
#import "SceneManager.h"

@interface MainViewController ()
{
    MenuListContent* menuList;
    CartTableView* cartTable;
    UIView* prevView;
}
@end

@implementation MainViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    CALayer* layer = [[self mainContent] layer];
    [layer setCornerRadius:10.0f];
    [layer setMasksToBounds:YES];
    [layer setBorderColor:[UIColor blackColor].CGColor];
    [layer setBorderWidth:1.0f];
    
    CGRect rect = [[self mainContent] frame];
    rect.origin.x = 0.0f;
    rect.origin.y = 0.0f;
    
    menuList = [[MenuListContent alloc] initWithFrame:rect];
    cartTable = [[CartTableView alloc] initWithFrame:rect];
    
    [[self mainContent] addSubview:menuList];
    prevView = menuList;
    [[self menuButton] setSelected:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [cartTable reloadCart];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)menuButtonClick:(id)sender {
    [[self menuButton] setSelected:YES];
    [[self cartButton] setSelected:NO];
    [[self dealsButton] setSelected:NO];
    [self setMenu];
}

- (IBAction)cartButtonClick:(id)sender {
    [[self menuButton] setSelected:NO];
    [[self cartButton] setSelected:YES];
    [[self dealsButton] setSelected:NO];
    [self setCart];
}

- (IBAction)dealsButtonClick:(id)sender {
    [[self menuButton] setSelected:NO];
    [[self cartButton] setSelected:NO];
    [[self dealsButton] setSelected:NO];
}

- (void) setMenu
{
    if (prevView == menuList) return;
    [UIView transitionFromView:prevView
                        toView:menuList
                      duration:0.5
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    completion:^(BOOL finished) {
                        if (finished) {
                            prevView = menuList;
                        }
                    }];
}

- (void) setCart
{
    if (prevView == cartTable) return;
    [UIView transitionFromView:prevView
                        toView:cartTable
                      duration:0.5
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    completion:^(BOOL finished) {
                        if (finished) {
                            prevView = cartTable;
                        }
                    }];
}

- (IBAction)checkoutButtonPressed:(id)sender {
    [[CartManager getInstance] sendOrder];
}

@end
